﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NestedGridView
{
    public partial class _Default : Page
    {
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        BindGrid();
}
private void BindGrid()
{
    var customerRepository = new CustomerRepository();
    var customers = customerRepository.GetCustomers(1 , 10);
    gvCustomers.DataSource = customers;
    gvCustomers.DataBind();
}
public string MyNewRow(object customerId)
{
    return String.Format(@"</td></tr><tr id ='tr{0}' class='collapsed-row'>
        <td></td><td colspan='100' style='padding:0px; margin:0px;'>", customerId);
}

protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
        var gvOrders = (GridView)e.Row.FindControl("gvOrders");

        var repository = new CustomerRepository();
        var orders = repository.GetCustomerOrders(customerId);
        gvOrders.DataSource = orders;
        gvOrders.DataBind();

    }
}
    }
}